package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;

import utility.Record;

import business.StatutEnseignantBL;
import business.TypeEnseignementBL;
import business.TypeHeureBL;

public class PanelStatutUI extends AbstractParamPane implements ActionListener {
	
    private StatutEnseignantBL statutBL;
	private JButton ajouter, modifier, supprimer;
    private JList list;
    
    
	public PanelStatutUI() {
	
		//Create and populate the panel.
        setLayout(new BorderLayout());
		setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createTitledBorder("Statuts"),
				BorderFactory.createEmptyBorder(5,5,5,5)));

		statutBL = new StatutEnseignantBL();
		list = new JList();
		refreshListe();
        
        ajouter = new JButton ("Ajouter");
        modifier= new JButton ("Modifier");
        supprimer = new JButton ("Supprimer");
        
        ajouter.addActionListener(this);
        modifier.addActionListener(this);
        supprimer.addActionListener(this);

        JPanel commandBox = new JPanel();
        commandBox.add(ajouter);
        commandBox.add(modifier);
        commandBox.add(supprimer);
        
        drawCommandBox(commandBox);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == ajouter) {
			PanelStatutUIConsulter panel = new PanelStatutUIConsulter(AbstractViewPane.ViewMode.AJOUT, -1, this);
			panel.setVisible(true);
		}
		if (e.getSource() == modifier) {
			if (list.getSelectedIndex() != -1) {
				PanelStatutUIConsulter panel = new PanelStatutUIConsulter(AbstractViewPane.ViewMode.MODIFICATION, this.getIdSelectedItem(), this);
				panel.setVisible(true);
			}
		}
		if (e.getSource() == supprimer) {
			if (list.getSelectedIndex() != -1) {
					loadSupprimerViewPane();
				}
			}
	}
	
	
	public int getIdSelectedItem(){
		return ((Record)list.getSelectedValue()).getId();
	}
	
	/**
	 * Permet la suppression d'un statut d'enseignant
	 */
	public void statutUISupprimer(){
		int retour =  JOptionPane.showConfirmDialog(
			    this,
			    "Êtes vous sûr de vouloir supprimer ce statut\n"+
			    //ensBL.getName()+" "+ensBL.getPrenom()+
			    " ?",
			    "Avertissement",
			    JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);	
		if (retour == JOptionPane.YES_OPTION) {
			StatutEnseignantBL statutASuppr = new StatutEnseignantBL(this.getIdSelectedItem());
			statutASuppr.supprimerStatutEnseignant();
		}	
	
	}
	
	
	
	public void refreshListe() {
		Vector<Record> listeRecord = new Vector<Record>();
		for(Record recordType : statutBL.getListeStatutEnseignant()) {
			listeRecord.add(recordType);
		}
		list.setListData(listeRecord);
        add(list);
	}
	
	
	public void loadSupprimerViewPane() {
		statutUISupprimer();
		refreshListe();
	}

}
